﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ajeelee.Core.Database;
using Ajeelee.Core.Utils;
using FxhjpgWeb.Business;
using FxhjpgWeb.Model;
using FxhjpgWeb.WebSite.Code;


namespace FxhjpgWeb.WebSite.en.pfdn
{
    public partial class newslist : PageBase
    {
        protected int pid;
        protected string title, cname, customwhere;
        Hashtable table = new Hashtable();
        protected IList<News> newList = new List<News>();
        INewsBusiness newBll = new NewsBusinessFactory().CreateNewsBusiness();
        protected PageStructure ps = new PageStructure() { CurrPage = 1, PageSize = 12 };

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            pid = QueryParamInt32("id");
            title = QueryParam("title");
            // 接收参数
            if (QueryParamInt32("page") > 1)
            {
                ps.CurrPage = QueryParamInt32("page");
            }

            try
            {
                // 读取分页数据
                if (pid > 0)
                {
                    table.Add(NewsHash.CId, pid);
                }
                else
                {
                    string cids = "";
                    foreach (var item in Utility.GetNewsClass().Where(c => c.PId == 1))
                    {
                        cids += (cids.IsNotNullAndEmpty() ? "," : "") + item.Id;
                    }
                    if (cids.IsNotNullAndEmpty())
                    {
                        customwhere = "N_CId in (" + cids + ")";
                    }
                }
                if (title.IsNotNullAndEmpty())
                {
                    table.Add(NewsHash.Title, Function.SafeSqlLiteral(title));
                }
                table.Add(NewsHash.CustomWhere, customwhere + (customwhere.IsNotNullAndEmpty() ? "and" : "") + " N_Title_En <> ''");
                newList = newBll.GetAllNewsByPage(table, ps);
                this.pager.PageStruct = ps;
                this.pager.TargetUrlFormat = "/pfdn/newslist.aspx?page={thispage}" + (pid > 0 ? "&id=" + pid : "");

                var citem = Utility.GetNewsClass().FirstOrDefault(c => c.Id == pid);
                if (citem != null)
                {
                    cname = citem.Name;
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }
    }
}